package quq.missq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.adapter.MyContractorAdapter;
import quq.missq.beans.Author;
import quq.missq.beans.AuthorListBean;
import quq.missq.beans.GoddessBean;
import quq.missq.utils.VolleyTool;

/**
 * Created by ghost on 15/9/21.
 */
public class AtNvShenListActivity extends BaseActivity implements VolleyTool.HTTPListener {
    private ListView lvSearch;
    private ArrayList<Author> authors;
    private MyContractorAdapter adapter;
    private static final int GET_GODDESS = 22;
    private View foot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nvshen_list_layout);
        initView();
        initData();
        initListener();
    }

    private void initData() {
        Map<String, String> params = new HashMap<>();
        params.put("pn", "0");
        params.put("maxResults", "50");

        VolleyTool.get(this, Constants.GET_GODDESS_URL, params, this, GET_GODDESS, GoddessBean.class);
    }

    private void initListener() {

        lvSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("author", adapter.getData().get(position));
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                AtNvShenListActivity.this.finish();
            }
        });
        lvSearch.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if(scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING){
                    if(lvSearch.getLastVisiblePosition() == lvSearch.getCount() - 1){
                        foot.setVisibility(View.VISIBLE);
                    }else{
                        foot.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
    }



    private void initView() {
        lvSearch = (ListView) findViewById(R.id.ll_search);
        foot = findViewById(R.id.foot);
        authors = new ArrayList<>();
        adapter = new MyContractorAdapter(this);
        adapter.setData(authors);
        lvSearch.setAdapter(adapter);

    }

    @Override
    public <T> void onResponse(T t, int requestCode) {

        if (requestCode == GET_GODDESS) {

            GoddessBean gb = (GoddessBean) t;
            if (gb.getCode() < 0) {
                showToast(gb.getMessage());
                return;
            }

            for (GoddessBean.Goddess g : gb.getData().getResults()) {
                authors.add(g.getUser());
            }
            adapter.notifyDataSetChanged();
        } else {
            AuthorListBean al = (AuthorListBean) t;
            if (al.getCode() < 0) {
                showToast(al.getMessage());
                return;
            }
            authors.clear();
            authors.addAll(al.getData().getResults());
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }
}
